<?php

class Category extends \Eloquent {
    protected $fillable = ['delete_flag'];
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Category';

    /**
     * @return array
     */
    public static function getList() {
        $results = DB::table('Category')->get(array('id', 'category_name'));

        foreach ($results as $item) {
            $data[$item->id] = $item->category_name;
        }

        return isset($data) ? $data: array();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public  static function getAll($conditions = array()) {
        $list = self::query();

        if (isset($conditions['keySearch']) && $conditions['keySearch']) {
            $list->where('category_name', 'LIKE', '%'.$conditions['keySearch'].'%');
        }

        $list ->where('delete_flag', '<>', Config::get('constants.STATUS.enabled'))
              ->orderBy('updated_at', 'DESC');

        return $list->paginate(Config::get('constants.PAGING_NUMBER'));
    }

    public static function getListMenu() {
        $result = self::query()
            ->where('status', '<>', 0)
            ->orderBy('updated_at', 'DESC');

        return $result->get();
    }

    public static function categoriesBestSell() {
        $result = self::query()
            ->where('status', '<>', 0)
            ->limit(3)
            ->orderBy('updated_at', 'DESC');

        return $result->get();
    }
}